package net.randomice.genchk.popup.actions;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import net.randomice.oawutil.Monitor;
import net.randomice.oawutil.OAWProgressMonitorWrapper;
import net.randomice.oawutil.OawEclipseProjectResourceLoader;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.emf.mwe.core.WorkflowRunner;
import org.eclipse.emf.mwe.core.resources.ResourceLoader;
import org.eclipse.emf.mwe.core.resources.ResourceLoaderFactory;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

public class EcoreGenerateAction implements IObjectActionDelegate {

	private IStructuredSelection selection;

	/**
	 * Constructor for Action1.
	 */
	public EcoreGenerateAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		Monitor.runWithMonitor("Generating basic ecore oAW checks ...",
				new IRunnableWithProgress() {

					public void run(IProgressMonitor monitor)
							throws InvocationTargetException,
							InterruptedException {
						monitor.beginTask("Initializing ...", 200);
						IFile file = (IFile) selection.getFirstElement();
						try {

							// configure properties passed to the workflow
							// engine
							IProject project = file.getProject();
							IJavaProject javaProject = (IJavaProject) project
									.getNature(JavaCore.NATURE_ID);
							IPath path = file.getParent().getFullPath();
							IPackageFragment element = javaProject
									.findPackageFragment(path);
							String basepkg = null == element ? "" : element
									.getElementName();
							File absoluteFile = file.getLocation().toFile()
									.getAbsoluteFile();
							String ecoreFile = absoluteFile.toString(); // .toURI().toURL()
							String outputDir = new File(absoluteFile
									.getParentFile(), "checks").toString();
							ClassLoader contextClassLoader = EcoreGenerateAction.class
									.getClassLoader();
							URL ecoreWorkflowURL = contextClassLoader
									.getResource("/genchk.oaw");
							String workflow = FileLocator.toFileURL(
									ecoreWorkflowURL).getFile();

							try {
								ResourceLoader resourceLoader = new OawEclipseProjectResourceLoader(
										project, true);
								ResourceLoaderFactory
										.setCurrentThreadResourceLoader(resourceLoader);

								monitor.worked(25);

								Generator.run(workflow, ecoreFile, basepkg,
										outputDir,
										new OAWProgressMonitorWrapper(monitor));
							} finally {
								monitor.worked(75);
								monitor.setTaskName("refreshing ressources ...");
								ResourceLoaderFactory
										.setCurrentThreadResourceLoader(null);
								project.refreshLocal(IResource.DEPTH_INFINITE,
										new SubProgressMonitor(monitor, 75));
							}
						} catch (OperationCanceledException e) {
							throw e;
						} catch (Throwable e) {
							throw new InvocationTargetException(e);
						}
					}

				});
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = (IStructuredSelection) selection;
	}

}
